/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function getHttpObject(){
    var xmlHttp = false;
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp=false;
        }
    }
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined'){
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

function confirmAction(id){
    var r=confirm("Do you want to change Status Order !!!");
    if (r==true){
        updateStatusOrder(id);
    } else {
        listOrder();
    }
}

function XMLwithXSTL(xmlFile, xsltFile,idLocation){
    var xmlDOM=new ActiveXObject ("Microsoft.XMLDOM");
    xmlDOM.async=false;
    xmlDOM.load(xmlFile);
    var xsltDOM=new ActiveXObject("Microsoft.XMLDOM");
    xsltDOM.async=false;
    xsltDOM.load(xsltFile);
    if (xmlDOM.parseError.errorCode!=0) {
        alert("Error: "+xmlDoc.parseError.reason);
    }else if (xsltDOM.parseError.errorCode!=0) {
        alert("Error: "+xmlDoc.parseError.reason);
    }else{
        var out=xmlDOM.transformNode(xsltDOM);
        document.getElementById(idLocation).innerHTML=out;
        
    }
}

function loadOrders(){
    var today = new Date();
    var mm = today.getMonth()+1;
    var yy = today.getFullYear();

    var loadOrder="<select name='Month' id='month'><option value='0'>All</option>";
    for(i=1; i<=12;i++){
        if(mm==i){
            loadOrder+="<option selected='selected' value='"+i+"'>"+i+"</option>";
        }else{
            loadOrder+="<option value='"+i+"'>"+i+"</option>";
        }
    }
    loadOrder+="</select> <select name='Year' id='year'>"
    +"<option value='"+(parseInt(yy)-2)+"'>"+(parseInt(yy)-2)+"</option>"
    +"<option value='"+(parseInt(yy)-1)+"'>"+(parseInt(yy)-1)+"</option>"
    +"<option selected='selected' value='"+parseInt(yy)+"'>"+parseInt(yy)+"</option>"
    +"</select><a href='#' onclick='listOrder()'  title='View'>"
    +"<img src='images/view.png' height='30px' style='margin-bottom:-7px'/>"
    +"</a><a href='#' onclick='printReport()'  title='Print PDF' style='float:right; margin-right:20px'>"
    +"<img src='images/product/pdf.jpg' height='50px'/>"
    +"</a><div id='showOrder'></div>";
    document.getElementById("content").innerHTML=loadOrder;
    listOrder();
}

function printReport(){
    window.open('PrintPDF?btAction=printPDF');
}

function listOrder(){
    var month;
    var year;
    month=document.getElementById("month").value;
    year=document.getElementById("year").value;
    xmlHttp=getHttpObject();
    if(xmlHttp){
        xmlHttp.open("POST", "AdminController", true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        var url="btAction=listOrder&txtMonth="+month+"&txtYear="+year;
        xmlHttp.send(url);
        xmlHttp.onreadystatechange=function(){
            if (xmlHttp.readyState==4) {
                if (xmlHttp.status==200) {
                    if(xmlHttp.responseText!=0){
                        XMLwithXSTL('XML/listOrder.xml', 'xsl/listOrder.xsl','showOrder');
                        if(month==0){
                            document.getElementById("pickMonth").innerHTML= "List of Orders in "+year;
                        }else {
                            document.getElementById("pickMonth").innerHTML= "List of Orders in "+month+"/"+year;
                        }
                    } else{
                        if(month==0){
                            document.getElementById("showOrder").innerHTML="<h1 style='color:red; padding-left:12px;'>Not found Orders in "+year+"</h1>";
                        } else {
                            document.getElementById("showOrder").innerHTML="<h1 style='color:red; padding-left:12px;'>Not found Orders in "+month+"/"+year+"</h1>";
                        }
                    }
                }
            }else{
        }
        }
    }
}

function updateStatusOrder(id){
    var status=document.getElementById("orderStatus"+id).value;
    var mm;
    var yy;
    mm=document.getElementById("month").value;
    yy=document.getElementById("year").value;
    xmlHttp=getHttpObject();
    if(xmlHttp){
        xmlHttp.open("POST", "AdminController", true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        var url="btAction=updateStatus&txtOrderID="+id+"&txtStatus="+status;
        xmlHttp.send(url);
        xmlHttp.onreadystatechange=function(){
            if (xmlHttp.readyState==4) {
                if (xmlHttp.status==200) {
                    alert(xmlHttp.responseText);
                    XMLwithXSTL('XML/listOrder.xml', 'xsl/listOrder.xsl','showOrder');
                    if(mm==0){
                        document.getElementById("pickMonth").innerHTML= "List of Orders in "+yy;
                    }else {
                        document.getElementById("pickMonth").innerHTML= "List of Orders in "+mm+"/"+yy;
                    }
                }
            }
        }
    }
}

function showOrderDetail(id){
    xmlHttp=getHttpObject();
    if(xmlHttp){
        xmlHttp.open("POST", "AdminController", true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        var url="btAction=showOrderDetail&txtOrderID="+id;
        xmlHttp.send(url);
        xmlHttp.onreadystatechange=function(){
            if (xmlHttp.readyState==4) {
                if (xmlHttp.status==200) {
                    XMLwithXSTL('XML/showOrderDetail.xml', 'xsl/showOrderDetail.xsl','content');
                }
            }
        }
    }
}
